package com.ecoolex.ying.space.api.graphql;

import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.GoodsBrand;
import com.ecoolex.ying.space.pojo.QueryGoodsBrandDTO;
import com.ecoolex.ying.space.ws.GoodsService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import java.util.List;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.User;

@GraphQL2Module(type = User, description = "商品品牌管理")
public class GoodsBrandGraphql {

    @Reference
    private GoodsService goodsService;
    @Autowired
    private StorageService storageService;

    @TypeField(type = GoodsBrand.class, field = "image", description = "图片")
    public StorageInfo image(@Source GoodsBrand source) {
        return storageService.get(source.getImage());
    }

    @TypeField(type = GoodsBrand.class, field = "logo", description = "图片")
    public StorageInfo logo(@Source GoodsBrand source) {
        return storageService.get(source.getLogo());
    }



    @QueryField(field = "goodsBrand", description = "获取商品品牌详情")
    public GoodsBrand get(@NonNull Integer brandId, ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.getBrandById(brandId);
    }

    @QueryField(field = "goodsBrandList", description = "查询商品品牌列表")
    public Pagination<GoodsBrand> queryBrand(@NonNull QueryGoodsBrandDTO query, @NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.queryBrand(query, sorts, page);
    }

    @QueryField(field = "goodsBrandAllList", description = "查询所有商品品牌列表")
    public List<GoodsBrand> queryAllBrand(ConsumerMemberAuthority user) {
        user.assertLogin();
        return goodsService.queryAllBrand();
    }

}
